package com.xcity.game.activity2.platform;

import java.util.EnumMap;
import java.util.Map;

import com.xcity.db.entity.Player;
import com.xcity.game.account.AccountChannel;
import com.xcity.game.common.IRegistry;
import com.xcity.game.common.MessageConst;
import com.xcity.pomelo.PomeloRoute;
import com.xcity.pomelo.request.PlatformGiftInfoRequest;
import com.xcity.pomelo.request.PlatformGiftRequest;
import com.xcity.pomelo.response.HintResponse;

import naga.x.net.Session;
import naga.x.net.annotation.OPHandler;
import naga.x.net.impl.pomelo.annotation.PomeloOP;
import naga.x.service.Service;
import naga.x.service.ServiceException;

/**
 * 平台礼包服务类，负责分发数据包
 * @author yang.li
 *
 */
@OPHandler
public class PlatformGiftDispatchService implements Service, IPacketHandler, IRegistry<AccountChannel, IPacketHandler> {
	
	private Map<AccountChannel, IPacketHandler> handlers = new EnumMap<AccountChannel, IPacketHandler>(AccountChannel.class);

	@Override
	public void startup() throws ServiceException {
	}

	@Override
	public void shutdown() throws ServiceException {
	}

	@Override
	public String getId() {
		return getClass().getName();
	}
	
	@Override
	public void register(AccountChannel channel, IPacketHandler handler) {
		handlers.put(channel, handler);
	}
	
	@Override
	public void unRegister(AccountChannel channel) {
		handlers.remove(channel);
	}
	
	@Override
	@PomeloOP(PomeloRoute.PLATFORM_GIFT_INFO)
	public void requestGiftInfo(Session session, PlatformGiftInfoRequest req) {
		AccountChannel channel = channelGet(session);
		IPacketHandler handler = handlers.get(channel);
		if (handler == null) {
			session.send(new HintResponse(req.getSerial(), MessageConst.ACTIVITY_THIRTY_GET_OVERTIME));
			return;
		}
		handler.requestGiftInfo(session, req);
	}
	
	@Override
	@PomeloOP(PomeloRoute.PLATFORM_GIFT_GET)
	public void requestGift(Session session, PlatformGiftRequest req) {
		AccountChannel channel = channelGet(session);
		IPacketHandler handler = handlers.get(channel);
		if (handler == null) {
			session.send(new HintResponse(req.getSerial(), MessageConst.ACTIVITY_THIRTY_GET_OVERTIME));
			return;
		}
		handler.requestGift(session, req);
	}
	
	private static AccountChannel channelGet(Session session) {
		Player player = session.getClient();
		if (player == null) {
			return AccountChannel.UNKNOWN;
		}
		return player.getAccount().getChannel();
	}

}
